﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Text;

namespace API.Controllers
{
    public class ReferralLogController : ApiController
    {
        /// <summary>
        /// Intializing Helper Classes - DB Connection and Security Provider for Encrypting and Decrypting Passwords
        /// Pass in DBServerName, DBUsern
        /// ame and DBPassword from Config File for DB connections
        /// The Security helper does nto take any parameters
        /// </summary>
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());

        [ActionName("ReferralLog")]
        public  ReferralLogResponse GetReferralLog(int id)
        {
            ReferralLogResponse response = new ReferralLogResponse();
            dsComm.AddParameter("@ParamReferralID", id);

            string data = dsComm.ExecuteStoredProcedure("dbo.spGetCurrentReferralLogs");
            XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
            DataSet LogDataSet = new DataSet();
            LogDataSet.ReadXml(xReader);

            if (LogDataSet.Tables.Count > 0)
            {
                response.LogMsg = LogDataSet.Tables[0];
            }
            else
            {
                response.ErrorMessage = "No Status Updates";
            }
            return response;
        }

        /// <summary>
        /// Options response for the preflight request of a CORS Opertaions. Will be standard across various functions
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage OptionsPatientDiagnosis()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }
    }

}
